package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import school.domain.Schedule;
import school.mapper.ScheduleMapper;
import school.support.BaseController;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Controller
@RequestMapping("schedule")
public class ScheduleController extends BaseController {

    @Autowired
    private ScheduleMapper scheduleMapper;

    /**
     * 日程列表
     *
     * @param schedule
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(Schedule schedule, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 10);
        Example example = new Example(Schedule.class);
        if(schedule.getName()!=null)
            example.createCriteria()
                    .andLike("name", "%" + schedule.getName() + "%");
        PageInfo<Schedule> pageInfo = new PageInfo<>(scheduleMapper.selectByExample(example), 5);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("requestUrl", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "schedule/list";
    }

    /**
     * 删除日程
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        scheduleMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    /**
     * 添加日程
     *
     * @return
     */
    @GetMapping("add")
    public String add() {
        return "schedule/add";
    }

    /**
     * 添加日程
     *
     * @return
     */
    @PostMapping("add")
    public String add(Schedule schedule) {
        schedule.setAddtime(new Date());
        scheduleMapper.insertSelective(schedule);
        return "redirect:list.do";
    }

    /**
     * 日程详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Schedule schedule = scheduleMapper.selectByPrimaryKey(id);
        model.addAttribute("schedule", schedule);
        return "schedule/detail";
    }

    /**
     * 更新日程信息
     *
     * @param schedule
     * @return
     */
    @PostMapping("update")
    public String update(Schedule schedule) {
        scheduleMapper.updateByPrimaryKeySelective(schedule);
        return "redirect:list.do";
    }

    /**
     * 日程列表
     *
     * @param schedule
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list-query")
    public String query(Schedule schedule, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 10);
        Example example = new Example(Schedule.class);
        if(schedule.getName()!=null)
            example.createCriteria()
                    .andLike("name", "%" + schedule.getName() + "%");
        PageInfo<Schedule> pageInfo = new PageInfo<>(scheduleMapper.selectByExample(example), 5);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("requestUrl", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "schedule/query";
    }
    
}
